/**
 * 
 */
package com.jeecms.wicool.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

/**
 * @author spring
 *
 */
public class SMSUtil {

	 /**
	 * 订单派单时发送给配送的短信内容
	 */
	public static String SMS_CONTENT_PEISONG = "Hi，您的美食已启程，途中若无劫匪，预计半小时送达。照顾好自己，少熬夜，多喝水，多看书。营养美食的事儿可以问我。 我是Kael, 卡餐美食顾问。【卡餐】";
	
	/**
	 * 短信接口URL
	 */
	public static String SMS_URL = "http://sms.chanzor.com:8001/sms.aspx";
	
	public static String account = "Kacan-tz";
	
	public static String password = "154209";
	
	public static String action = "send";
	
	public static String extno = "8003";
	
	
	public static String peisongsend(String mobile) throws UnsupportedEncodingException{
		String params = "account="+account+"&password="+password+"&action="+action+"&mobile="+mobile
				+"&content="+URLEncoder.encode(SMS_CONTENT_PEISONG, "UTF-8");
		System.out.println("SMS URL:"+SMS_URL);
		System.out.println("SMS params:"+params);
		return SMS("", SMS_URL+"?"+params);
	}
	
	public static String SMS(String postData, String postUrl) {
        try {
            //发送POST请求
            URL url = new URL(postUrl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            conn.setRequestProperty("Connection", "Keep-Alive");
            conn.setUseCaches(false);
            conn.setDoOutput(true);

            conn.setRequestProperty("Content-Length", "" + postData.length());
            OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
            out.write(postData);
            out.flush();
            out.close();

            //获取响应状态
            if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
                System.out.println("connect failed!");
                return "";
            }
            //获取响应内容体
            String line, result = "";
            BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            while ((line = in.readLine()) != null) {
                result += line + "\n";
            }
            in.close();
            return result;
        } catch (IOException e) {
            e.printStackTrace(System.out);
        }
        return "";
    }
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(peisongsend("15822079375"));
	}
}
